<template>
  <!-- 搜索区域 -->
  <ContentWrap>
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="90px"
    >
      <el-form-item label="应用名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入应用名"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="社交平台" prop="socialType">
        <el-select
          v-model="queryParams.socialType"
          placeholder="请选择社交平台"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in common_social_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="用户类型" prop="userType">
        <el-select
          v-model="queryParams.userType"
          placeholder="请选择用户类型"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in common_user_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="客户端编号" prop="clientId">-->
<!--        <el-input-->
<!--          v-model="queryParams.clientId"-->
<!--          placeholder="请输入客户端编号"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in common_enable_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['system:social-client:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
              type="success"
              plain
              @click="handleExport"
              :loading="exportLoading"
              v-hasPermi="['system:social-client:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表区域 -->
  <ContentWrap>
    <el-table v-loading="listLoading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="编号" prop="id" width="100" />
      <el-table-column label="应用名" prop="name" />
      <el-table-column label="社交平台" prop="socialType">
        <template #default="scope">
          <dict-tag :options="common_social_type" :value="scope.row.socialType" />
        </template>
      </el-table-column>
      <el-table-column label="用户类型" prop="userType" width="150">
        <template #default="scope">
          <dict-tag :options="common_user_type" :value="scope.row.userType" />
        </template>
      </el-table-column>
      <el-table-column label="客户端编号" prop="clientId" />
      <el-table-column label="agentId" prop="agentId" />
      <el-table-column label="状态" prop="status" width="150">
        <template #default="scope">
          <dict-tag :options="common_enable_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="创建时间" prop="createTime" width="180px" />
      <el-table-column label="操作" width="160px" fixed="right">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['system:social-client:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['system:social-client:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页区域 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <SocialClientForm ref="editFormRef" @success="getList" />
</template>

<script setup lang="ts">
import { SocialClientApi, SocialClientVO } from '@/api/system/social/social_client'
import SocialClientForm from './SocialClientForm.vue'
import download from '@/utils/download'

/** 社交客户端 列表 */
defineOptions({ name: 'SystemSocialClient' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const { common_user_type } = useDict( DICT_TYPE.common_user_type )
const { common_social_type, common_enable_status } = useIntDict( DICT_TYPE.common_social_type, DICT_TYPE.common_enable_status )

const listLoading = ref(true) // 列表的加载中状态
const list = ref<SocialClientVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  name: undefined,
  socialType: undefined,
  userType: undefined,
  clientId: undefined,
  status: undefined,
  pageNo: 1,
  pageSize: 10
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  listLoading.value = true
  try {
    const data = await SocialClientApi.getSocialClientPage(queryParams)
    list.value = data.content
    total.value = data.totalElement
  } finally {
    listLoading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const editFormRef = ref<InstanceType<typeof SocialClientForm>>()
const openForm = (type: string, id?: number) => {
  editFormRef.value?.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 弹出确认框
    await message.delConfirm()
    // 发起删除请求
    await SocialClientApi.deleteSocialClient(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await SocialClientApi.exportSocialClient(queryParams)
    download.excel(data, '社交客户端数据.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>

<style scoped lang="scss"></style>
